package com.hy.mvc.biz.excel.model.factory;

import com.hy.mvc.biz.excel.model.SimpleExcelModel;
import com.hy.mvc.biz.utils.PropertiesUtils;

/**
 * Created by Booker on 2017/7/3.
 */
public class ExcelModelFactory {
    private static final String DEFAULT_FILE_NAME_KEY = "fileName";

    private static final String DEFAULT_TITLE_KEY = "title";

    private static final String DEFAULT_HEADER_KEY = "header";

    private static final String DEFAULT_EXPORT_FIELDS_KEY = "exportFields";

    private static final String DEFAULT_REGEX = ",";

    private static final String DEFAULT_PROPERTY_LOCATION = "/exportTemplete/exportTemplete.properties";

    public static SimpleExcelModel createExcelModel(String url, String headerRegex, String exportFieldsRegex,
                                                    String fileNameKey, String titleKey, String headerKey,
                                                    String exportFieldsKey) {
        SimpleExcelModel excelModel = new SimpleExcelModel();

        String fileName = PropertiesUtils.get(url, fileNameKey);
        excelModel.setFileName(fileName);

        String title = PropertiesUtils.get(url, titleKey);
        excelModel.setTitle(title);

        String[] header = PropertiesUtils.get(url, headerKey).split(headerRegex);
        excelModel.setHeader(header);

        String[] exportField = PropertiesUtils.get(url, exportFieldsKey).split(exportFieldsRegex);
        excelModel.setExportFields(exportField);

        return excelModel;
    }

    public static SimpleExcelModel createExcelModel(String url, String headerRegex, String exportFieldsRegex) {
        return createExcelModel(url, headerRegex, exportFieldsRegex,
                DEFAULT_FILE_NAME_KEY, DEFAULT_TITLE_KEY, DEFAULT_HEADER_KEY,
                DEFAULT_EXPORT_FIELDS_KEY);
    }

    public static SimpleExcelModel createExcelModel(String url, String fileNameKey, String titleKey, String headerKey,
                                                    String exportFieldsKey) {
        return createExcelModel(url, DEFAULT_REGEX, DEFAULT_REGEX, fileNameKey, titleKey, headerKey, exportFieldsKey);
    }

    public static SimpleExcelModel createExcelModel(String url) {
        return createExcelModel(url, DEFAULT_REGEX, DEFAULT_REGEX);
    }

    public static SimpleExcelModel createExcelModelFromDefaultLocation(String prefix) {
        String fileNameKey = prefix + "." + DEFAULT_FILE_NAME_KEY;
        String titleKey = prefix + "." + DEFAULT_TITLE_KEY;
        String headerKey = prefix + "." + DEFAULT_HEADER_KEY;
        String exportFieldsKey = prefix + "." + DEFAULT_EXPORT_FIELDS_KEY;

        return createExcelModel(DEFAULT_PROPERTY_LOCATION, DEFAULT_REGEX,
                DEFAULT_REGEX, fileNameKey, titleKey, headerKey, exportFieldsKey);
    }
}
